﻿//<TunynetCopyright>
//--------------------------------------------------------------
//<copyright>青岛拓宇网络科技有限公司 2005-2008</copyright>
//<version>V0.5</verion>
//<createdate>2008-4-21</createdate>
//<author>xier</author>
//<email>xier@tunynet.com</email>
//<log date="2008-4-21">创建</log>
//--------------------------------------------------------------
//</TunynetCopyright>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;
using Tunynet.QingdaoCustoms.Caching;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Collections;

namespace ChinaCustoms.Qingdao.Workflow.Permissions
{
    /// <summary>
    /// 通用授权平台包装器
    /// </summary>
    public class ChinaCustomsPermissionWrapper
    {

        private static bool isUseCache = true;
        /// <summary>
        /// 是否使用缓存
        /// </summary>
        public static bool IsUseCache
        {
            get
            {
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["IsUseCache"]))
                {
                    bool.TryParse(ConfigurationManager.AppSettings["IsUseCache"], out isUseCache);
                }
                return isUseCache;
            }
        }

        private static int cacheMinute = 10;
        /// <summary>
        /// 缓存的分钟数
        /// </summary>
        public static int CacheMinute
        {
            get
            {
                if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["CacheMinute"]))
                {
                    int.TryParse(ConfigurationManager.AppSettings["CacheMinute"], out cacheMinute);
                }
                return cacheMinute;
            }
        }

        /// <summary>
        /// 获取指定角色,部门内的人员列表
        /// </summary>
        /// <param name="roleNames">角色的CodeName</param>
        /// <param name="applicationName">应用的CodeName</param>
        /// <param name="orgnizationIDs">部门ID数组</param>
        /// <returns></returns>
        public static List<IUser> GetUsers(string applicationName, string[] roleNames, string[] orgnizationIDs)
        {
            return GetUsers(applicationName,roleNames,orgnizationIDs,true);
        }

        /// <summary>
        /// 获取指定角色,部门内的人员列表
        /// </summary>
        /// <param name="roleNames">角色的CodeName</param>
        /// <param name="applicationName">应用的CodeName</param>
        /// <param name="orgnizationIDs">部门ID数组</param>
        /// <param name="isExcludeSameUserID">如果有相同的人员ID,是否要进行排除</param>
        /// <returns></returns>
        public static List<IUser> GetUsers(string applicationName, string[] roleNames, string[] orgnizationIDs,bool isExcludeSameUserID)
        {
            string roleNameString = string.Empty;
            if (roleNames != null && roleNames.Length > 0)
                roleNameString = string.Join("+", roleNames);

            string orgnizationIDString = string.Empty;
            if (orgnizationIDs != null && orgnizationIDs.Length > 0)
                orgnizationIDString = string.Join("+", orgnizationIDs);

            string cacheKey = string.Format("Users.{0}.{1}.{2}.{3}", applicationName, roleNameString, orgnizationIDString,isExcludeSameUserID.ToString());

            List<IUser> users = null;

            //1.判断缓存,从缓存内直接返回对象
            if (IsUseCache)
            {
                users = WebCache.Get(cacheKey) as List<IUser>;
                if (users != null)
                {
                    return users;
                }
            }

            //2.如果缓存内没有对象,则从数据库获取
            if (users == null)
            {
                users = new List<IUser>();

                if (roleNames == null || roleNames.Length == 0)
                {
                    if (orgnizationIDs == null || orgnizationIDs.Length == 0)
                    {
                        IOrganization rootOrg = GetRootOrganization(); //OguMechanismFactory.GetMechanism().GetRoot();
                        orgnizationIDs = new string[1];
                        orgnizationIDs[0] = rootOrg.ID;
                    }

                    ReadOnlyCollection<IUser> _users = GetUsersInOrganizations(orgnizationIDs);
                    if (_users != null && _users.Count > 0)
                    {
                        users = new List<IUser>(_users);
                    }
                }
                else
                {
                    OguObjectCollection<IOrganization> orgColl = null;
                    if (orgnizationIDs == null || orgnizationIDs.Length == 0)
                    {
                        IOrganization rootOrg = GetRootOrganization(); //OguMechanismFactory.GetMechanism().GetRoot();
                        orgColl = OguMechanismFactory.GetMechanism().GetObjects<IOrganization>(SearchOUIDType.Guid, rootOrg.ID);
                    }
                    else
                    {
                        orgColl = OguMechanismFactory.GetMechanism().GetObjects<IOrganization>(SearchOUIDType.Guid, orgnizationIDs);
                    }

                    ApplicationCollection appColl = PermissionMechanismFactory.GetMechanism().GetApplications(applicationName);
                    if (appColl == null)
                    {
                        return null;
                    }

                    RoleCollection roleColl = appColl[0].Roles.GetSpecifiedRoles(roleNames);

                    OguObjectCollection<IOguObject> tempUsers = PermissionMechanismFactory.GetMechanism().GetRolesObjects(roleColl, orgColl, true);
                    if (tempUsers == null)
                        return null;

                    for (int i = 0; i < tempUsers.Count; i++)
                    {
                        IUser user = tempUsers[i] as IUser;
                        if (user != null)
                            users.Add(user);
                    }
                }
            }

            if (users == null)
            {
                return null;
            }
            else
            {
                if (isExcludeSameUserID == true)
                {
                    List<IUser> doneUsers = new List<IUser>();
                    Dictionary<string, IUser> dic = new Dictionary<string, IUser>();
                    for (int i = 0; i < users.Count; i++)
                    {
                        if (dic.ContainsKey(users[i].ID) == false)
                        {
                            dic.Add(users[i].ID, users[i]);
                        }
                    }

                    foreach (KeyValuePair<string, IUser> kvp in dic)
                    {
                        doneUsers.Add(kvp.Value);
                    }

                    if (IsUseCache)
                    {
                        WebCache.Insert(cacheKey, doneUsers, WebCache.MinuteFactor * CacheMinute);
                    }
                    return doneUsers;
                }
                else
                {
                    return users;
                }
            }
        }



        /// <summary>
        /// 获取根部门
        /// </summary>
        public static IOrganization GetRootOrganization()
        {
            string cacheKey = string.Format("ChinaCustoms.Permission.GetRootOrganization");

            IOrganization rootOrg = null;

            if (IsUseCache)
                rootOrg = WebCache.Get(cacheKey) as IOrganization;

            if (rootOrg == null)
            {
                ////由于通用授权平台内的获取根节点有问题,暂时采用自己的方法
                //rootOrg = OguMechanismFactory.GetMechanism().GetRoot();

                string tempRootID = ConfigurationManager.AppSettings["OGURootID"];
                if (string.IsNullOrEmpty(tempRootID))
                {
                    rootOrg = OguMechanismFactory.GetMechanism().GetRoot();
                }
                else
                {
                    string[] parentOrgnizationIDs = new string[1];
                    parentOrgnizationIDs[0] = tempRootID;
                    OguObjectCollection<IOrganization> parentColl = OguMechanismFactory.GetMechanism().GetObjects<IOrganization>(SearchOUIDType.Guid, parentOrgnizationIDs);
                    if (parentColl != null && parentColl.Count > 0)
                    {
                        rootOrg = parentColl[0];
                    }
                }


                if (IsUseCache)
                {
                    WebCache.Insert(cacheKey, rootOrg, WebCache.MinuteFactor * CacheMinute);
                }
            }

            return rootOrg;
        }

        /// <summary>
        /// 获取应用内的角色集合
        /// </summary>
        /// <param name="applicationName">应用名称代码</param>
        /// <returns></returns>
        public static ReadOnlyCollection<IRole> GetRolesInApplication(string applicationName)
        {
            string cacheKey = string.Format("ChinaCustoms.Permission.GetRolesInApplication.{0}", applicationName);

            if (IsUseCache == true)
            {
                ReadOnlyCollection<IRole> dic = WebCache.Get(cacheKey) as ReadOnlyCollection<IRole>;
                if (dic == null)
                {
                    dic = GetRolesInApplicationInFact(applicationName);
                    WebCache.Insert(cacheKey, dic, WebCache.MinuteFactor * CacheMinute);
                }
                return dic;
            }
            else
            {
                return GetRolesInApplicationInFact(applicationName);
            }
        }

        private static ReadOnlyCollection<IRole> GetRolesInApplicationInFact(string applicationName)
        {
            ApplicationCollection appColl = PermissionMechanismFactory.GetMechanism().GetApplications(applicationName);
            if (appColl != null && appColl.ContainsKey(applicationName) == true)
            {
                IApplication app = appColl[applicationName];
                RoleCollection roleColl = app.Roles;

                IList<IRole> list = new List<IRole>();
                for (int i = 0; i < roleColl.Count; i++)
                {
                    list.Add(roleColl[i]);
                }

                return new ReadOnlyCollection<IRole>(list);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取所有的应用系统
        /// </summary>
        /// <returns></returns>
        public static ReadOnlyCollection<IApplication> GetApplications()
        {
            string cacheKey = string.Format("ChinaCustoms.Permission.GetApplications");

            if (IsUseCache == true)
            {
                ReadOnlyCollection<IApplication> dic = WebCache.Get(cacheKey) as ReadOnlyCollection<IApplication>;
                if (dic == null)
                {
                    dic = GetApplicationsInFact();
                    WebCache.Insert(cacheKey, dic, WebCache.MinuteFactor * CacheMinute);
                }
                return dic;
            }
            else
            {
                return GetApplicationsInFact();
            }
        }

        private static ReadOnlyCollection<IApplication> GetApplicationsInFact()
        {
            ApplicationCollection appColl = PermissionMechanismFactory.GetMechanism().GetAllApplications();

            IList<IApplication> list = new List<IApplication>();
            foreach (IApplication app in appColl)
            {
                list.Add(app);
            }

            ReadOnlyCollection<IApplication> rcColl = new ReadOnlyCollection<IApplication>(list);
            return rcColl;
        }

        /// <summary>
        /// 获取应用
        /// </summary>
        /// <param name="applicationCodeName">应用的英文代码</param>
        /// <returns></returns>
        public static IApplication GetApplication(string applicationCodeName)
        {
            string cacheKey = string.Format("ChinaCustoms.Permission.GetApplication.{0}", applicationCodeName);

            if (IsUseCache == true)
            {
                IApplication IApplication = WebCache.Get(cacheKey) as IApplication;
                if (IApplication == null)
                {
                    IApplication = GetApplicationInFact(applicationCodeName);
                    WebCache.Insert(cacheKey, IApplication, WebCache.MinuteFactor * CacheMinute);
                }
                return IApplication;
            }
            else
            {
                return GetApplicationInFact(applicationCodeName);
            }
        }

        private static IApplication GetApplicationInFact(string applicationCodeName)
        {
            ApplicationCollection appColl = PermissionMechanismFactory.GetMechanism().GetApplications(applicationCodeName);

            if (appColl != null && appColl.Count > 0)
            {
                return appColl[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取所有的部门
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, IOrganization> GetAllOrganizations()
        {
            string cacheKey = string.Format("ChinaCustoms.Permission.GetAllOrganizations");

            if (IsUseCache == true)
            {
                Dictionary<string, IOrganization> dic = WebCache.Get(cacheKey) as Dictionary<string, IOrganization>;
                if (dic == null)
                {
                    dic = GetAllOrganizationsInFact();
                    WebCache.Insert(cacheKey, dic, WebCache.MinuteFactor * CacheMinute);
                }
                return dic;
            }
            else
            {
                return GetAllOrganizationsInFact();
            }
        }

        private static Dictionary<string, IOrganization> GetAllOrganizationsInFact()
        {
            IOrganization orgRoot = GetRootOrganization(); //OguMechanismFactory.GetMechanism().GetRoot();
            OguObjectCollection<IOrganization> orgColl = orgRoot.GetAllChildren<IOrganization>(true);

            Dictionary<string, IOrganization> dic = new Dictionary<string, IOrganization>(orgColl.Count);

            foreach (IOrganization org in orgColl)
            {
                dic.Add(org.ID, org);
            }

            return dic;
        }

        /// <summary>
        /// 通过父部门ID获取其下所有的子部门
        /// </summary>
        /// <param name="parentOrgnizationID">父部门ID</param>
        /// <returns></returns>
        public static Dictionary<string, IOrganization> GetOrganizations(string parentOrgnizationID)
        {
            return GetOrganizations(parentOrgnizationID, true);
        }

        /// <summary>
        /// 通过父部门ID获取其下所有的子部门
        /// </summary>
        /// <param name="parentOrgnizationID">父部门ID</param>
        /// <param name="isRecursive">是否要递归查询</param>
        /// <returns></returns>
        public static Dictionary<string, IOrganization> GetOrganizations(string parentOrgnizationID, bool isRecursive)
        {
            string cacheKey = string.Format("ChinaCustoms.Permission.GetOrganizations.{0}.{1}", parentOrgnizationID, isRecursive.ToString());

            if (IsUseCache == true)
            {
                Dictionary<string, IOrganization> dic = WebCache.Get(cacheKey) as Dictionary<string, IOrganization>;
                if (dic == null)
                {
                    dic = GetOrganizationsInFact(parentOrgnizationID, isRecursive);
                    WebCache.Insert(cacheKey, dic, WebCache.MinuteFactor * CacheMinute);
                }
                return dic;
            }
            else
            {
                return GetOrganizationsInFact(parentOrgnizationID, isRecursive);
            }
        }

        private static Dictionary<string, IOrganization> GetOrganizationsInFact(string parentOrgnizationID, bool isRecuresive)
        {
            string[] parentOrgnizationIDs = new string[1];
            parentOrgnizationIDs[0] = parentOrgnizationID;
            OguObjectCollection<IOrganization> parentColl = OguMechanismFactory.GetMechanism().GetObjects<IOrganization>(SearchOUIDType.Guid, parentOrgnizationIDs);
            if (parentColl != null && parentColl.Count > 0)
            {
                Dictionary<string, IOrganization> dic = null;
                if (isRecuresive == true)
                {
                    OguObjectCollection<IOrganization> orgColl = parentColl[0].GetAllChildren<IOrganization>(true);

                    dic = new Dictionary<string, IOrganization>(orgColl.Count);
                    foreach (IOrganization org in orgColl)
                    {
                        dic.Add(org.ID, org);
                    }
                }
                else
                {
                    OguObjectCollection<IOguObject> oguColl = parentColl[0].Children;
                    dic = new Dictionary<string, IOrganization>();
                    for (int j = 0; j < oguColl.Count; j++)
                    {
                        if (oguColl[j].ObjectType == SchemaType.Organizations)
                        {
                            IOrganization org = oguColl[j] as IOrganization;
                            dic.Add(org.ID, org);
                        }
                    }

                }
                return dic;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 通过部门ID获取部门信息
        /// </summary>
        /// <param name="organizationID">部门ID</param>
        /// <returns></returns>
        public static IOrganization GetOrganization(string organizationID)
        {
            string cacheKey = string.Format("ChinaCustoms.Permission.GetOrganizationByID.{0}", organizationID);

            if (IsUseCache == true)
            {
                IOrganization dic = WebCache.Get(cacheKey) as IOrganization;
                if (dic == null)
                {
                    dic = GetOrganizationByIDInFact(organizationID);
                    WebCache.Insert(cacheKey, dic, WebCache.MinuteFactor * CacheMinute);
                }
                return dic;
            }
            else
            {
                return GetOrganizationByIDInFact(organizationID);
            }
        }

        private static IOrganization GetOrganizationByIDInFact(string organizationID)
        {
            OguObjectCollection<IOrganization> orgColl = OguMechanismFactory.GetMechanism().GetObjects<IOrganization>(SearchOUIDType.Guid, organizationID);
            if (orgColl != null && orgColl.Count > 0)
            {
                return orgColl[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取一组部门下的人员列表
        /// </summary>
        /// <param name="organizationIDs">部门的ID</param>
        /// <returns></returns>
        public static ReadOnlyCollection<IUser> GetUsersInOrganizations(params string[] organizationIDs)
        {
            return GetUsersInOrganizations(organizationIDs, SearchOUIDType.Guid, true);
        }

        /// <summary>
        /// 获取一组部门下的人员列表
        /// </summary>
        /// <param name="organizationIDs">部门的ID</param>
        /// <param name="isRecursive">是否要递归查询</param>
        /// <returns></returns>
        public static ReadOnlyCollection<IUser> GetUsersInOrganizations(string[] organizationIDs, bool isRecursive)
        {
            return GetUsersInOrganizations(organizationIDs, SearchOUIDType.Guid, isRecursive);
        }

        /// <summary>
        /// 获取一组部门下的人员列表
        /// </summary>
        /// <param name="organizationFullNames">部门的全路径名称</param>
        /// <returns></returns>
        public static ReadOnlyCollection<IUser> GetUsersInOrganizationsByName(string[] organizationFullNames)
        {
            return GetUsersInOrganizations(organizationFullNames, SearchOUIDType.FullPath, true);
        }

        /// <summary>
        /// 获取一组部门下的人员列表
        /// </summary>
        /// <param name="organizationFullNames">部门的全路径名称</param>
        /// <param name="isRecursive">是否要递归查询</param>
        /// <returns></returns>
        public static ReadOnlyCollection<IUser> GetUsersInOrganizationsByName(string[] organizationFullNames, bool isRecursive)
        {
            return GetUsersInOrganizations(organizationFullNames, SearchOUIDType.FullPath, isRecursive);
        }

        private static ReadOnlyCollection<IUser> GetUsersInOrganizations(string[] inputs, SearchOUIDType searchType, bool isRecursive)
        {
            string inputsString = string.Empty;
            foreach (string input in inputs)
            {
                inputsString += "+" + input;
            }

            string cacheKey = string.Format("ChinaCustoms.Permission.GetUsersInOrganizations.{0}.{1}.{2}", inputsString, searchType.ToString(), isRecursive.ToString());

            if (IsUseCache == true)
            {
                ReadOnlyCollection<IUser> dic = WebCache.Get(cacheKey) as ReadOnlyCollection<IUser>;
                if (dic == null)
                {
                    dic = GetUsersInOrganizationsInFact(inputs, searchType, isRecursive);
                    WebCache.Insert(cacheKey, dic, WebCache.MinuteFactor * CacheMinute);
                }
                return dic;
            }
            else
            {
                return GetUsersInOrganizationsInFact(inputs, searchType, isRecursive);
            }
        }


        /// <summary>
        /// 这个方法能递归获取到下级部门的人员
        /// </summary>
        /// <param name="inputs"></param>
        /// <param name="searchType"></param>
        /// <returns></returns>
        private static ReadOnlyCollection<IUser> GetUsersInOrganizationsInFact(string[] inputs, SearchOUIDType searchType, bool isRecursive)
        {
            OguObjectCollection<IOrganization> orgColl = OguMechanismFactory.GetMechanism().GetObjects<IOrganization>(SearchOUIDType.Guid, inputs);

            IList<IUser> list = new List<IUser>();
            for (int i = 0; i < orgColl.Count; i++)
            {
                if (isRecursive == true)
                {
                    OguObjectCollection<IUser> oguColl = orgColl[i].GetAllChildren<IUser>(true);
                    for (int j = 0; j < oguColl.Count; j++)
                    {
                        list.Add(oguColl[j]);
                    }
                }
                else
                {
                    OguObjectCollection<IOguObject> oguColl = orgColl[i].Children;
                    for (int j = 0; j < oguColl.Count; j++)
                    {
                        if (oguColl[j].ObjectType == SchemaType.Users)
                        {
                            IUser user = oguColl[j] as IUser;
                            list.Add(user);
                        }
                    }
                }
            }

            return new ReadOnlyCollection<IUser>(list);
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="userName">用户登录名称</param>
        /// <returns></returns>
        public static IUser GetUser(string userName)
        {
            return GetUser(userName, SearchOUIDType.LogOnName);
        }
        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns></returns>
        public static IUser GetUserByID(string userID)
        {
            return GetUser(userID, SearchOUIDType.Guid);
        }

        private static IUser GetUser(string inputInfo, SearchOUIDType searchType)
        {
            string cacheKey = string.Format("ChinaCustoms.Permission.GetUser.{0}.{1}", inputInfo, searchType.ToString());

            if (IsUseCache == true)
            {
                IUser dic = WebCache.Get(cacheKey) as IUser;
                if (dic == null)
                {
                    dic = GetUserInFact(inputInfo, searchType);
                    WebCache.Insert(cacheKey, dic, WebCache.MinuteFactor * CacheMinute);
                }
                return dic;
            }
            else
            {
                return GetUserInFact(inputInfo, searchType);
            }
        }

        private static IUser GetUserInFact(string inputInfo, SearchOUIDType searchType)
        {
            OguObjectCollection<IUser> userColl = OguMechanismFactory.GetMechanism().GetObjects<IUser>(searchType, inputInfo);

            if (userColl == null || userColl.Count == 0)
            {
                return null;
            }
            else
            {
                IUser user = userColl[0];
                return user;
            }
        }


        /// <summary>
        /// 获取某部门下,组织好的带关系的部门集合
        /// </summary>
        /// <param name="rootOrg">顶级部门</param>
        /// <param name="NeedDisplayOrganizationCollection">带父子关系的部门集合</param>
        /// <param name="NeedDisplayorganizationCollectionAll">所有的部门集合</param>
        public static void GetObjectsSearched(IOrganization rootOrg, out Dictionary<string, string> NeedDisplayOrganizationCollection,
            out Dictionary<string, IOrganization> NeedDisplayorganizationCollectionAll)
        {
            /// <summary>
            /// 所有的部门
            /// </summary>
            Dictionary<string, IOrganization> organizationCollectionAll = new Dictionary<string, IOrganization>();
            /// <summary>
            /// 部门间的关系
            /// </summary>
            /// <remarks>第一个string表示部门的ID,第二个string表示父部门的ID</remarks>
            Dictionary<string, string> organizationRelationship = new Dictionary<string, string>();

            /// <summary>
            /// 需要搜索的部门集合,即需要显示在树上的部门(未处理的，即还没有排定组织的结构关系)
            /// </summary>
            /// <remarks>第一个string表示部门的ID,第二个bool(true显示下面的全部用户,flase显示角色中指定的用户)</remarks>
            Dictionary<string, string> organizationCollectionSearchedUndone = new Dictionary<string, string>();

            /// <summary>
            /// 需要搜索的部门集合,即需要显示在树上的部门(以处理的，即已经排定组织的结构关系)
            /// </summary>
            /// <remarks>第一个string表示部门的ID,第二个bool(true显示下面的全部用户,flase显示角色中指定的用户)</remarks>
            Dictionary<string, string> organizationCollectionSearchedDone = new Dictionary<string, string>();

            organizationCollectionAll = GetOrganizationCollectionAll(rootOrg);
            organizationRelationship = GetOrganizationRelationship(organizationCollectionAll);


            organizationCollectionSearchedUndone = organizationRelationship;
            organizationCollectionSearchedDone = organizationCollectionSearchedUndone;
            NeedDisplayOrganizationCollection = organizationCollectionSearchedDone;
            NeedDisplayorganizationCollectionAll = organizationCollectionAll;
        }

        private static Dictionary<string, IOrganization> GetOrganizationCollectionAll(IOrganization rootOrg)
        {
            if (rootOrg == null)
            {
                rootOrg = GetRootOrganization();
            }

            Dictionary<string, IOrganization> organizationCollectionAll = GetOrganizations(rootOrg.ID);
            return organizationCollectionAll;
        }

        /// <summary>
        /// 设置部门间的关系
        /// </summary>
        /// <remarks>即是让部门ID跟其父ID对应</remarks>
        private static Dictionary<string, string> GetOrganizationRelationship(Dictionary<string, IOrganization> organizationCollectionAll)
        {
            Dictionary<string, string> organizationRelationship = new Dictionary<string, string>();
            if (organizationCollectionAll != null)
            {
                int orgCount = organizationCollectionAll.Count;

                foreach (KeyValuePair<string, IOrganization> pair in organizationCollectionAll)
                {
                    organizationRelationship.Add(pair.Key, pair.Value.Parent.ID);
                }
            }

            return organizationRelationship;
        }

        /// <summary>
        /// 把需要显示用户所在的部门 添加到未处理的部门集合中
        /// </summary>
        /// <param name="organizationCollectionSearchedUndone"></param>
        /// <param name="usersNeedDisplay"></param>
        private static void AddOrganizationCollectionSearchedUndone(ref Dictionary<string, string> organizationCollectionSearchedUndone, ReadOnlyCollection<IUser> usersNeedDisplay)
        {
            if (usersNeedDisplay == null)
            {
                return;
            }


            for (int i = 0; i < usersNeedDisplay.Count; i++)
            {
                if (usersNeedDisplay[i].Parent != null && organizationCollectionSearchedUndone.ContainsKey(usersNeedDisplay[i].Parent.ID) == false)
                {
                    if (usersNeedDisplay[i].Parent.Parent == null)
                    {
                        organizationCollectionSearchedUndone.Add(usersNeedDisplay[i].Parent.ID, null);
                    }
                    else
                    {
                        organizationCollectionSearchedUndone.Add(usersNeedDisplay[i].Parent.ID, usersNeedDisplay[i].Parent.Parent.ID);
                    }
                }
            }
        }

        private static void AddOrganizationCollectionSearchedUndone(ref Dictionary<string, string> organizationCollectionSearchedUndone, string[] searchedOrgIDs, Dictionary<string, string> organizationRelationship)
        {
            for (int i = 0; i < searchedOrgIDs.Length; i++)
            {
                if (organizationCollectionSearchedUndone.ContainsKey(searchedOrgIDs[i]) == false && organizationRelationship.ContainsKey(searchedOrgIDs[i]) == true)
                {
                    organizationCollectionSearchedUndone.Add(searchedOrgIDs[i], organizationRelationship[searchedOrgIDs[i]]);
                }
            }
        }
    }
}
